package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.Log;
import com.fbee.smartcommunities.mapper.LogMapper;
import com.fbee.smartcommunities.service.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-19
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Resource
    private LogMapper logMapper;

    @Override
    public Boolean keepLog(String content, Long id, String ip) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        return this.save(new Log(content,LocalDateTime.now(),id,ip));
    }

    @Override
    public Page<Log> getPage(Integer pageNo, Integer pageSize, String keyword) {
        Page<Log> logPage = new Page<>(pageNo, pageSize);
        QueryWrapper<Log> logQueryWrapper = new QueryWrapper<>();
        if(keyword == null){
            return logMapper.selectPage(logPage,null);
        } else {
            logQueryWrapper.like("time",keyword).or().eq("id",keyword).or().eq("ip",keyword);
            return logMapper.selectPage(logPage,logQueryWrapper);
        }
    }

}
